package com.yykj.common.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yykj.common.enums.ResultEnums;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * Created by chendg on 2021/9/8 0008.
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultVO<T> implements Serializable {

	@ApiModelProperty(value = "编号,0成功，其它失败")
	private Integer code;

	@ApiModelProperty(value = "code 不为0时，返回错误信息")
	private String msg;

	@ApiModelProperty(value = "返回对象")
	private T data;

	public static <T> ResultVO success(T data) {
		ResultVO<T> resultVO = new ResultVO<>();
		resultVO.setData(data);
		resultVO.setCode(0);
		resultVO.setMsg("成功");
		return resultVO;
	}

	public static ResultVO success() {
		return success(null);
	}

	public static <T> ResultVO error(T data) {
		ResultVO<T> resultVO = new ResultVO<>();
		resultVO.setData(data);
		resultVO.setCode(100);
		resultVO.setMsg("失败");
		return resultVO;
	}

	public static ResultVO error(ResultEnums resultEnums) {
		ResultVO resultVO = new ResultVO<>();
		resultVO.setCode(resultEnums.getCode());
		resultVO.setMsg(resultEnums.getMessage());
		return resultVO;
	}

	public static ResultVO error(ResultEnums resultEnums, String message) {
		ResultVO resultVO = new ResultVO<>();
		resultVO.setCode(resultEnums.getCode());
		resultVO.setMsg(message);
		return resultVO;
	}

	public static ResultVO error(Integer code, String message) {
		ResultVO resultVO = new ResultVO<>();
		resultVO.setCode(code);
		resultVO.setMsg(message);
		return resultVO;
	}

}
